from pydantic import BaseModel, Field, validator
from datetime import date
from enum import Enum


class Gender(str, Enum):
    MALE = "male"
    FEMALE = "female"
    OTHER = "other"


class StudentCreate(BaseModel):
    # ... 在 Pydantic 中用作标记，表示该字段需要被验证为非空值。
    name: str = Field(..., min_length=2, max_length=20, description="姓名长度需在2到20个字符之间")
    age: int = Field(..., gt=0, lt=150, description="年龄需在0到150岁之间")
    comment: str = Field(None, description="备注")  # 默认值为 None
    birth_date: date
    gender: Gender

    @validator('birth_date')
    def birth_date_not_future(cls, v):
        from datetime import datetime
        if v > datetime.now().date():
            raise ValueError('出生日期不能是未来日期')
        return v


class StudentUpdate(BaseModel):
    name: str = None
    age: int = None
    birth_date: date = None
    gender: Gender = None


class StudentResponse(BaseModel):
    id: int
    name: str
    age: int
    birth_date: date
    gender: Gender

    class Config:
        orm_mode = True
